package com.jsyh.maidu.presenter;

import android.content.Context;

import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.http.BaseDelegate;
import com.jsyh.maidu.http.OkHttpClientManager;
import com.jsyh.maidu.model.ArticleListModel;
import com.jsyh.maidu.model.ArticleModel;
import com.jsyh.maidu.views.ArticleView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZHY_9 on 2017/7/12.
 */

public class ArticlePresenter extends BasePresenter {

    private ArticleView articleView;

    public ArticlePresenter(ArticleView articleView) {
        this.articleView = articleView;
    }

    public void getArticles(Context context) {
        initLoadDialog(context);
        mLoadingDialog.show();
        OkHttpClientManager.getAsyn(context, ConfigValue.APP_IP + "first/article_cates", new BaseDelegate.ResultCallback<ArticleModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(ArticleModel response, Object tag) {
                mLoadingDialog.dismiss();
                articleView.getArticles(response);
            }
        });
    }

    public void getArticleList(Context context, String cat_id, String page, String keyword) {
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String, String> param = new HashMap<>();
        param.put("cat_id", cat_id);
        param.put("page", page);
        param.put("keyword", keyword);
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "first/articles", param, new BaseDelegate.ResultCallback<ArticleListModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(ArticleListModel response, Object tag) {
                mLoadingDialog.dismiss();
                articleView.getArticleList(response);
            }
        });
    }

}
